// pages/recharge/recharge.js
import {
    getbaseUrl,
    requestUtil
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        rechargeNum:[50,100,200,500,1000],
        recharActive:0,
        Value:"",
        money:50,
        show:false,
        warnning:"",
        warnning:"宏鼎烘焙(连锁店)",
        plase:"",
        
        userInfo:{},
        isPayPassword:0,
        initshow:false,
        initwarnning:"为了您的资金安全，请初始密码",
        submitPassword:0
    },

    // 获取你点击的金额
    getMoney(e){
        let {index,money} = e.currentTarget.dataset
        this.setData({
            recharActive:index,
            money
        })
    },

    // 改变active
    changeActive(){
        this.setData({
            recharActive:520
        })
    },

    // 输入框的数值
    inpValue(e){
        let reg = /^[0-9]*$/
        let value =e.detail.value
      
        if(reg.test(value)){
            this.setData({
                Value:value,
                money:value
            })
        }else{
            this.setData({
                Value:""
            })
        }
        if(value==""){
            this.setData({
                money:50
            })
        }
       
    },

    

    // 充值金额
    rechargeMoney(){
        let isPayPassword = this.data.isPayPassword
            if(isPayPassword==0){
                this.setData({
                    initshow: true
                })
            }else{
                let money = parseInt(this.data.money)
                // console.log(money)
                this.setData({
                    plase:`￥${money}`,
                    show:true
                })
            }

    },

   // 密码组件的回调
   initsubmit(e){
    let openId = this.data.userInfo.openid
    let value = e.detail.value
    let submitPassword = this.data.submitPassword
    if(value!==submitPassword){
        this.setData({
            initwarnning:"请再次输入密码",
            submitPassword:value
        })
    }else{
        // console.log("密码一致")
        let res = requestUtil({
            url:"/shop/insertPaypassword",
            method:"get",
            data:{
                openId,
               password:submitPassword
            }
        })
        res.then(({data})=>{
            // console.log(data)
            if(data.status==200){
                this.setData({
                    initwarnning:"为了您的资金安全，请初始密码",
                    initshow:false
                })
                this.getUserPayPassword()
            }else{
                this.setData({
                    initwarnning:"为了您的资金安全，请初始密码",
                    plase:"密码初始化失败，请再试",
                    initshow:true,
                    submitPassword:0
                })
            }
        })
      
    }
   
},


    // 获取用户的密码
    getUserPayPassword() {
        let openId = this.data.userInfo.openid

        let res = requestUtil({
            url: "/shop/getUerPayPassword",
            method: "get",
            data: {
                openId
            }
        })

        res.then(({
            data
        }) => {
            // console.log(data)
            if(data.status==200){
                // console.log(data.data)
                this.setData({
                    isPayPassword:data.data
                })
            }else{
                this.setData({
                    isPayPassword:data.data
                })
            }
        })

    },


    // 密码框的回调函数
    submit(e){
        wx.showLoading({
          title: '数据加载中',
        })
        let password = e.detail.value
        let openId = this.data.userInfo.openid
        let money = this.data.money
        let res = requestUtil({
            url:"/shop/matchPayPassword",
            method:"get",
            data:{
                password,
                openId,
                money
            }
            
        })
        res.then(({data})=>{
            wx.hideLoading()
            console.log(data)
            if(data.data==0){
                wx.showToast({
                  title: '密码错误',
                })
            }

            if(data.data==1){
                wx.showToast({
                    title: '充值成功',
                  })
                  this.setData({
                    recharActive:0,
                      show:false
                  })
            }
            if(data.data==2){
                wx.showToast({
                    title: '充值失败',
                  })
            }
        })
    
        console.log(password)
    },

    // 获取用户的信息
    getUserInfo(){
        wx.getStorage({
            key:"userInfo",
            success:(res)=>{
                
                this.setData({
                    userInfo:JSON.parse(res.data)
                })
                this.getUserPayPassword()
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})